//
//  ZLScheduleViewController.m
//  LittleFairyPoet
//
//  Created by rimi on 15/9/8.
//  Copyright (c) 2015年 周林. All rights reserved.
//

#import "ZLScheduleViewController.h"
#import "WPAddPlanViewController.h"
#import "WPAddPlanViewModel.h"
#import "Plan.h"

static NSString *const kUITableViewCellIdentifier = @"planCellIdentifier";

@interface ZLScheduleViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView * planTabview;
@property (nonatomic, strong) NSMutableArray * planDataSource;

- (void)initializeDataSource; /**< 初始化数据源 */
- (void)initializeUserInterface; /**< 初始化用户界面 */

@end

@implementation ZLScheduleViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initializeUserInterface];
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self initializeDataSource];
}
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    if (self.planDataSource.count == 0) {
        [self initializeAlertControllerWithMessage:@"您还没有添加学习计划喔，快点击右上角新建吧"];
    }
}
#pragma mark - private methods
- (void)initializeDataSource {
    self.planDataSource = [WPAddPlanViewModel readPlan];
    [self.view addSubview:self.planTabview];
    [self.planTabview reloadData];
}
- (void)initializeUserInterface {
    self.view.backgroundColor = [UIColor whiteColor];
    self.titleLable.text = @"学习计划";
    [self.rightButton setImage:IMAGE_CONTENT(@"添加按钮.png") forState:UIControlStateNormal];
    self.baseNavigationBar.backgroundColor = [UIColor clearColor];
    CALayer *layer = [CALayer layer];
    layer.bounds = self.view.bounds;
    layer.position = self.view.center;
    layer.contents = (__bridge id)(IMAGE_CONTENT(@"计划背景.jpg")).CGImage;
    [self.view.layer addSublayer:layer];
    [self.baseNavigationBar removeFromSuperview];
    [self.view addSubview: self.baseNavigationBar];
}
#pragma mark - responds events
- (void)respondsToTabBarRightButton:(UIButton *)sender {
    WPAddPlanViewController * findVC = [[WPAddPlanViewController alloc]init];
    [self.navigationController pushViewController:findVC animated:YES];
}
#pragma mark - 系统协议
#pragma mark---协议UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _planDataSource.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kUITableViewCellIdentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:kUITableViewCellIdentifier];
    }
    cell.accessoryType = UITableViewCellAccessoryNone;
    cell.textLabel.text = _planDataSource[indexPath.row][0];
    cell.detailTextLabel.text = _planDataSource[indexPath.row][1];
    cell.backgroundColor = [UIColor clearColor];
    return cell;
}
//删除标题设置
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {
    return @"删除";
}
//处理删除或者添加逻辑
//在这个方法中可以获取当前选中的indexpath(section, row)
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [WPAddPlanViewModel deleteSignalPlanWithPlanTime:self.planDataSource[indexPath.row][0]];
    [self.planDataSource removeObject:_planDataSource[indexPath.row]];
    [self.planTabview reloadData];
}
//是否允许拖动
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
    Plan * plan = [_planDataSource[sourceIndexPath.row] copy];
    [_planDataSource removeObjectAtIndex:sourceIndexPath.row];
    [_planDataSource insertObject:plan atIndex:destinationIndexPath.row];
    [self.planTabview reloadData];
}

#pragma mark - UITableViewDataSource
//设置每一行高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 60;
}
//用户点击了某一行
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    WPAddPlanViewController * addVC = [[WPAddPlanViewController alloc]init];
    addVC.model.planTime = self.planDataSource[indexPath.row][0];
    addVC.model.createTime = self.planDataSource[indexPath.row][1];
    addVC.model.peoms = self.planDataSource[indexPath.row][2];
    addVC.model.topTime = self.planDataSource[indexPath.row][3];
    addVC.model.topOn = self.planDataSource[indexPath.row][4];
    [self.navigationController pushViewController:addVC animated:YES];
}
#pragma mark - getter
- (UITableView *)planTabview {
    if (!_planTabview) {
        _planTabview = ({
            UITableView * tableview = [[UITableView alloc]initWithFrame:DHFlexibleFrame(CGRectMake(0, 64, ORIGIN_WIDTH, ORIGIN_HEIGHT-64), NO) style:UITableViewStylePlain];
            tableview.dataSource = self;
            tableview.delegate = self;
            tableview.tableFooterView = [UIView new];
            tableview.pagingEnabled = NO;
            tableview.separatorColor = [UIColor blackColor];
            tableview.backgroundColor = [UIColor clearColor];
            tableview;
        });
    }
    return _planTabview;
}
- (NSMutableArray *)planDataSource {
    if (!_planDataSource) {
        _planDataSource = [NSMutableArray array];
    }
    return _planDataSource;
}
@end
